package com.jzo2o.market.controller.operation;

import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @Author anjing
 * @Date 2024/12/15 16:57
 */
@RestController
@RequestMapping("/operation/activity")
@Api(tags = "运营端 - 优惠劵相关接口")
public class ActivityController {

    @Resource
    private IActivityService activityService;
    @Resource
    private ICouponService couponServicee;

    @PostMapping("/save")
    @ApiOperation("保存优惠劵活动")
    public void activitySave(@RequestBody ActivitySaveReqDTO activitySaveReqDTO){

        activityService.saveActivity(activitySaveReqDTO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询优惠劵活动查询")
    public PageResult<ActivityInfoResDTO> page( ActivityQueryForPageReqDTO activityQueryForPageReqDTO){
        return activityService.activityPage(activityQueryForPageReqDTO);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询具体优惠劵活动")
    public ActivityInfoResDTO getActivityById(@PathVariable Long id){
        return activityService.getActivityById(id);
    }

    @PostMapping("/revoke/{id}")
    @ApiOperation("撤销活动接口")
    public void  revokr(@PathVariable Long id){
        boolean update = activityService.lambdaUpdate().eq(Activity::getId, id).
                set(Activity::getStatus, ActivityStatusEnum.LOSE_EFFICACY.getStatus())
                .set(Activity::getIsDeleted,true).update();
        if (!update){
            throw  new BadRequestException("撤销失败");
        }
        boolean update1 = couponServicee.lambdaUpdate().eq(Coupon::getActivityId, id)
                .eq(Coupon::getStatus, CouponStatusEnum.NO_USE).set(Coupon::getStatus, CouponStatusEnum.VOIDED)
                .update();
        if (!update1){
            throw  new BadRequestException("撤销失败");
        }
    }


}
